#pragma once

#include <TNamed.h>
#include <vector>
#include <cstdint>

//单个cluster数据：
class TofCluster : public TNamed{
public:
    uint16_t timeOffset{};            // 时间窗口内偏移
    uint32_t coarseTime{};            // 粗时间
    uint16_t fineTime{};              // 细时间
    
    // TofCluster()
    //     : TNamed("TofCluster", "Tof Cluster"),
    //       timeOffset(0), coarseTime(0), fineTime(0) {}

    TofCluster(uint16_t offset = 0, uint32_t coarse = 0, uint16_t fine = 0)
        : TNamed("TofCluster", "Tof Cluster"), timeOffset(offset), 
        coarseTime(coarse), fineTime(fine) {}

    void Clear(Option_t* = "") override {
        coarseTime = fineTime = timeOffset = 0;
    }

    ClassDefOverride(TofCluster, 1);
};

// 单个通道数据：包含多个cluster
class TofChannel : public TNamed {
public:
    int channelId{};                 // 通道号
    std::vector<TofCluster> Cluster;

    TofChannel(int id = 0)
        : TNamed(Form("Ch%d", int(id)), "Tof channel"), channelId(id){}

    void Clear(Option_t* = "") override {
        channelId = 0;
        Cluster.clear();
    }

    ClassDefOverride(TofChannel, 1);
};

// 一个FEE，包含不定个数通道
class TofFEE : public TNamed {
public:
    uint8_t feeId{};
    std::vector<TofChannel> Channel;

    TofFEE(uint8_t id = 0)
        : TNamed(Form("FEE%d", int(id)), "Tof FEE board"), feeId(id) {}

    void Clear(Option_t* = "") override {
        feeId = 0;
        Channel.clear();
    }

    TofChannel* GetOrCreateChannel(uint8_t id) {
        for (auto& ch : Channel)
            if (ch.channelId == id)
                return &ch;
        Channel.emplace_back(id);
        return &Channel.back();
    }

    ClassDefOverride(TofFEE, 1);
};

// 一个PXI，包含多个FEE
class TofPXI : public TNamed {
public:
    uint8_t pxiId{};
    std::vector<TofFEE> Fee;

    TofPXI(uint8_t id = 0)
        : TNamed(Form("PXI%d", int(id)), "Tof PXI board"), pxiId(id) {}

    void Clear(Option_t* = "") override {
        pxiId = 0;
        Fee.clear();
    }

    ClassDefOverride(TofPXI, 1);
};

// 一个时间片，包含多个PXI
class TofData : public TNamed {
public:
    uint64_t TimeSliceNumber{};     // 时间片编号
    std::vector<TofPXI> Pxi; // 该时间片的所有PXI数据

    TofData(uint64_t sn = 0)
        : TNamed(Form("Slice%d", int(sn)), "Tof timeslice"), TimeSliceNumber(sn) {}

    void Clear(Option_t* = "") override {
        TimeSliceNumber = 0;
        Pxi.clear();
    }

    ClassDefOverride(TofData, 1);
};
